package com.zhongmeng.rxjava2test.utils

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.os.Environment
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.ListAdapter
import android.widget.ListView
import android.widget.Toast

import com.zhongmeng.rxjava2test.MyApplication


/**
 * author : rcw
 * time   : 2017/07/30 14:55
 * version: 1.0
 * desc   : UI计算工具类
 */
class UIUtils private constructor() {

    init {
        /* cannot be instantiated */
        throw UnsupportedOperationException("cannot be instantiated")
    }

    companion object {

        /**
         * @param context 上下文
         * @return 是否有网络连接
         */
        fun isNetWorkConnected(context: Context?): Boolean {
            if (context != null) {
                val mConnectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
                val mNetworkInfo = mConnectivityManager.activeNetworkInfo
                if (mNetworkInfo != null) {
                    return mNetworkInfo.isAvailable && mNetworkInfo.isConnected
                }
            }
            return false
        }

        /**
         * Toast 提示信息
         *
         * @param msg 提示内容
         */
        fun toast(msg: String) {
            val message = if (TextUtils.isEmpty(msg)) "未知提示信息" else msg
            if (!TextUtils.isEmpty(msg)) {
                Toast.makeText(MyApplication.instance, message, Toast.LENGTH_SHORT).show()
            }
        }

        /**
         * 检查设备是否存在SDCard的工具方法
         */
        fun hasSdcard(): Boolean {
            val state = Environment.getExternalStorageState()
            return if (state == Environment.MEDIA_MOUNTED) {
                // 有存储的SDCard
                true
            } else {
                false
            }
        }

        /**
         * 将px值转换为dip或dp值，保证尺寸大小不变
         *
         * @param pxValue （DisplayMetrics类中属性density）
         * @return dp值
         */
        fun px2dip(context: Context, pxValue: Float): Int {
            val scale = context.resources.displayMetrics.density
            return (pxValue / scale + 0.5f).toInt()
        }

        /**
         * 将dip或dp值转换为px值，保证尺寸大小不变
         *
         * @param dipValue dp值
         * @return 像素值
         */
        fun dip2px(context: Context, dipValue: Float): Int {
            val scale = context.resources.displayMetrics.density
            return (dipValue * scale + 0.5f).toInt()
        }

        fun SetHeigth(list: ListView) {
            val listAdapter = list.adapter ?: return
            var totalHeight = 0
            var i = 0
            val len = listAdapter.count
            while (i < len) {
                val listItem = listAdapter.getView(i, null, list)
                listItem.measure(LinearLayout.LayoutParams.MATCH_PARENT, 0)
                //listItem.measure(0, 0);
                totalHeight += listItem.measuredHeight
                i++
            }
            val params = list.layoutParams
            params.height = totalHeight + list.dividerHeight * (listAdapter.count - 1)
            list.layoutParams = params
        }
    }

}
